home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / putenv.c < prev    next >
C/C++ Source or Header  |  1992-05-15  |  1KB  |  71 lines

  1. /* functions for manipulating the environment */
  2. /* written by Eric R. Smith and placed in the public domain */
  3. /* 5/5/92 sb -- separated for efficiency, see also getenv.c */
  4.  
  5. #include <stddef.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. extern char ** environ;
  10.  
  11. static void
  12. del_env(strng)
  13.     const char *strng;
  14. {
  15.     char **var;
  16.     char *name;
  17.     size_t len = 0;
  18.  
  19.     if (!environ) return;
  20.  
  21. /* find the length of "tag" in "tag=value" */
  22.     for (name = (char *)strng; *name && (*name != '='); name++)
  23.         len++;
  24.  
  25. /* find the tag in the environment */
  26.     for (var = environ; name = *var; var++) {
  27.         if (!strncmp(name, strng, len) && name[len] == '=')
  28.             break;
  29.     }
  30.  
  31. /* if it's found, move all the other environment variables down by 1 to
  32.    delete it
  33.  */
  34.     if (name) {
  35.         while (name) {
  36.             name = var[1];
  37.             *var++ = name;
  38.         }
  39.     }
  40. }
  41.  
  42. int
  43. putenv(strng)
  44.     char *strng;
  45. {
  46.     int i = 0;
  47.     char **e;
  48.  
  49.     del_env(strng);
  50.  
  51.     if (!environ)
  52.         e = (char **) malloc(2*sizeof(char *));
  53.     else {
  54.         while(environ[i]) i++ ;
  55.         e = (char **) malloc((i+2)*sizeof(char *));
  56.         if (!e) {
  57.             return -1;
  58.         }
  59.         bcopy(environ, e, (i+1)*sizeof(char *));
  60.         free(environ);
  61.         environ = e;
  62.     }
  63.     if (!e)
  64.         return -1;
  65.  
  66.     environ = e;
  67.     environ[i] = strng;
  68.     environ[i+1] = 0;
  69.     return 0;
  70. }
  71.